package com.yin.ui;

import java.io.IOException;

import javax.swing.JOptionPane;

import org.lwjgl.input.Keyboard;

import com.yin.Session;
import com.yin.Yin;
import com.yin.verify.Util;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;

public class GuiVerify extends GuiScreen {

	private final GuiScreen previousScreen;
	private GuiTextField hwidField;
	private GuiButton exit;
	private GuiButton loginButton;
	public static String hwids;

	public GuiVerify(GuiScreen previousScreen) {
        this.previousScreen = previousScreen;
    }

	@Override
	protected void actionPerformed(GuiButton button) {
		switch (button.id) {
		case 0: {
			
			hwids = this.hwidField.getText();
			boolean result = Session.login();
			if(result) {
				
				JOptionPane.showMessageDialog(null, "验证成功！", "来自Yin Client的消息", JOptionPane.INFORMATION_MESSAGE);
				Minecraft.getMinecraft().displayGuiScreen(previousScreen);
				
				//隐藏Verify按钮
				for(GuiButton btn : previousScreen.getButtonList()) {
					if(btn.id == 14 && Yin.session != null) {
						
						btn.displayString = "You have verified";
						btn.enabled = false;
					}
				}
			} else {
				
				JOptionPane.showMessageDialog(null, "验证失败，请联系管理员", "来自Yin Client的消息", JOptionPane.ERROR_MESSAGE);
				JOptionPane.showInputDialog(null, "Hwid:", Util.getHWID());
			}
			
			break;
			
		}
		case 1: {
		
			Minecraft.getMinecraft().displayGuiScreen(previousScreen);
			
		}
		}
	}

	@Override
	public void drawScreen(int x2, int y2, float z2) {
		FontRenderer font = this.mc.fontRendererObj;
		this.drawDefaultBackground();
		this.hwidField.drawTextBox();
		this.drawCenteredString(font, "You need to verify", this.width / 2, 20, -1);
		if (this.hwidField.getText().isEmpty()) {
			this.drawString(font, "Hwid", this.width / 2 - 96, 66, -7829368);
		}
		super.drawScreen(x2, y2, z2);
	}

	@Override
	public void initGui() {
		FontRenderer font = this.mc.fontRendererObj;
		int var3 = this.height / 4 + 24;
		this.loginButton = new GuiButton(0, this.width / 2 - 100, 140, 80, 20, "Verify");
		this.exit = new GuiButton(1, this.width / 2 - 100 + 120, 140, 80, 20, "Return");
		this.buttonList.add(this.loginButton);
		this.buttonList.add(this.exit);
		this.hwidField = new GuiTextField(var3, font, this.width / 2 - 100, 60, 200, 20);
		this.hwidField.setFocused(true);
		Keyboard.enableRepeatEvents((boolean) true);
		
		this.hwidField.setText(Util.getHWID());
	}

	@Override
	protected void keyTyped(char character, int key) {
		try {
			super.keyTyped(character, key);
		} catch (IOException var4) {
			var4.printStackTrace();
		}
		if (character == '\t') {
			this.hwidField.setFocused(!this.hwidField.isFocused());
		}
		if (character == '\r') {
			this.actionPerformed((GuiButton) this.buttonList.get(0));
		}
		this.hwidField.textboxKeyTyped(character, key);
	}

	@Override
	protected void mouseClicked(int x2, int y2, int button) {
		try {
			super.mouseClicked(x2, y2, button);
		} catch (IOException var5) {
			var5.printStackTrace();
		}
		this.hwidField.mouseClicked(x2, y2, button);
	}

	@Override
	public void onGuiClosed() {
		Keyboard.enableRepeatEvents((boolean) false);
	}

	@Override
	public void updateScreen() {
		this.hwidField.updateCursorCounter();
	}
}
